package cn.edu.ctbu.labtest.webapi;

import cn.edu.ctbu.labtest.core.PageUtils;
import cn.edu.ctbu.labtest.domain.Student;
import cn.edu.ctbu.labtest.domain.Teacher;
import cn.edu.ctbu.labtest.helper.R;
import cn.edu.ctbu.labtest.helper.RUtil;
import cn.edu.ctbu.labtest.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/webapi/student")
public class SelfRestController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/self")
    public int self(HttpServletRequest httprequest, Model model, String username, String password) {
       //学生登录
        List<Student> student = studentService.findByNameAndPassword(username, password);
        if (student.size() > 0) {
            HttpSession session = httprequest.getSession();
            session.setAttribute("userid", student.get(0).getId());
            session.setAttribute("user", student.get(0));
            //session.setAttribute("user", student);
            //model.addAttribute("user", student);
            return 1;
        }
        else {
            return 0;
        }
    }

    @GetMapping("/slist")
    public List<Student> getSelf(String name,String password){
        List<Student> student=studentService.findByNameAndPassword(name, password);
        return student;
    }



//    public List<Student> slist(HttpServletRequest httprequest,Model model, String username, String password) {
//        //学生登录
//        List<Student> student = studentService.findByNameAndPassword(username, password);
//        if (student.size() > 0) {
//            HttpSession session = httprequest.getSession();
//            session.setAttribute("userid", student.get(0).getId());
//            session.setAttribute("user", student);
//            model.addAttribute("user", student);
//            return student;
//        }
//        else {
//            return null;
//        }
//    }

//    @RequestMapping("/self")
//    public List<Student> self(HttpServletRequest httprequest, Model model, String username, String password, Integer radioVal) {
//        if (radioVal == 1) {//学生登录
//            List<Student> student = studentService.findByNameAndPassword(username, password);
//            if (student.size() > 0) {
//                HttpSession session = httprequest.getSession();
//                session.setAttribute("user", student);
//                model.addAttribute("user", student);
//                return student;
//            }
//            else {
//                return null;
//            }
//        }
//        else{
//            return null;
//        }
//    }

//    @GetMapping("/findbyname")
//    public R<Student> findByName(String name){
//        List<Student> all;
//        all=studentService.findByName(name);
//        return RUtil.success(all);
//    }
//
//    //    @GetMapping("/getSelf")
////    public List<Student> getSelf(String username){
////
////    }
//
//    /**
//     *读取的url:/webapi/student/self
//     */
//    @GetMapping("/self")
//    public List<Student> self(String username) {
//        List<Student> students=studentService.findByName(username);
//        return students;
//    }

//    @GetMapping("/self")
//    public String user(HttpServletRequest httprequest, String username, String password, Integer radioVal) {
//        //判断用户名和密码是否存在
//
//        if (radioVal == 1) {//学生登录
//            List<Student> students = studentService.findByNameAndPassword(username, password);
//            if (students.size() > 0) {
//                //如果存在用户名和密码，写入session
//                HttpSession session = httprequest.getSession();
//                session.setAttribute("userid", students.get(0).getId());
//                session.setAttribute("user", students.get(0));
//                return "slist.html"; //成功
//            } else {
//                //如果不存在返回出错
//                //登录用户不存在
//                return "请输入账号密码";
//            }
//        }

//        else{//未选择登录用户类型
//            return -2;
//        }



}
